import { _decorator, Component, Node, sys, Button, AudioClip, AudioSource } from 'cc';
import { GameManager } from './GameManager';
import { SettingPanel } from './SettingPanel';
const { ccclass, property } = _decorator;

@ccclass('WinPanel')
export class WinPanel extends Component {
    @property(Button)
    restartButton: Button = null; // 重新开始游戏按钮

    @property(Button)
    downloadButton: Button = null; // 下载按钮

    @property(Node)
    settingPanel: Node | null = null; // 设置面板

    @property({ type: AudioClip, tooltip: '胜利音效剪辑' })
    winSoundClip: AudioClip | null = null; // 胜利音效剪辑

    private audioSource: AudioSource | null = null;

    onLoad() {
        // 添加 AudioSource 组件
        this.audioSource = this.node.addComponent(AudioSource);
    }

    start() {
        // 绑定重新开始游戏按钮点击事件
        this.restartButton.node.on(Button.EventType.CLICK, this.restartGame, this);

        // 绑定下载按钮点击事件
        this.downloadButton.node.on(Button.EventType.CLICK, this.openDownloadUrl, this);
    }

    // 重新开始游戏方法
    restartGame() {
        GameManager.getInstance().restartGame();
        this.node.active = false; // 隐藏面板
    }

    // 打开下载链接方法
    openDownloadUrl() {
        sys.openURL('https://pan.baidu.com/s/1PFqGcW9h1d9ZHdp6Cucing?pwd=8sqy');
    }

    // 显示面板方法
    showPanel() {
        console.log('显示胜利面板');
        this.node.active = true;
        this.settingPanel.active = false; // 禁用设置面板

        if (this.audioSource && this.winSoundClip) {
            this.audioSource.clip = this.winSoundClip;
            this.audioSource.play();
        }
    }

    // 隐藏面板方法
    hidePanel() {
        this.settingPanel.active = true; // 启用设置面板
        this.node.active = false;
    }
}